﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player; 
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.strategy;

namespace Pixysoft.Weblications.Sango.Command.strategy
{
    partial class SangoStrategyHandler
    {
        public strategy_upgrade strategy_upgrade(long serialcode)
        {
            string userCode = SangoManager.Authentication();

            strategy_upgrade response = ReflectionManager.CreatePojo<strategy_upgrade>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);

            Entities.Usr_Strategy strategy = StrategyDao.getStrategy(userCode, serialcode);

            Entities.Cti_Strategy cityStrategy = CityStrategyHelper.searchStrategy(strategy.Strategytypecode);

            Entities.Usr_Building building = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.INSTITUTE);

            Entities.Usr_Activity history = UsrActivityDao.getHistory(userCode);

            long timespan = StrategyFormular.getStrategyCooldownTimespan(history, cityStrategy, strategy.Strategylevel + 1);

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.TECH_UPGRADE, userCode, timespan);

            if (queue == null)
            {
                response.Message = "队列冷却中";
                return response;
            }

            if (strategy.Strategylevel >= building.Buildinglevel)
            {
                response.Message = "科技无法超过太学院等级";
                return response;
            }

            long soulvalue = StrategyFormular.getStragetyUpgradeSoulvalue(cityStrategy, strategy.Strategylevel);

            if (!UsrProfileDao.tryDecreaseSoulValue(profile, soulvalue))
            {
                response.Message = "军魂不足";
                return response;
            }

            strategy.Strategylevel = (strategy.Strategylevel + 1);
            strategy.Strategyvalue = (cityStrategy.Strategyvalue * strategy.Strategylevel);

            // 先升级战略，再升级阵法 所以带入参数不需要再+1
            List<Entities.Usr_Tacticalpos> openPoss = new List<Pixysoft.Weblications.Sango.Entities.Usr_Tacticalpos>();
            switch (strategy.Strategytypecode)
            {
                case CtiStrategyTypecodeCollection.STRATEGY_11_FENGSHI:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_01, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_12_YULIN:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_02, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_13_YUNLONG:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_03, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_14_XUANRANG:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_04, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_15_HEYI:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_05, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_16_YANXING:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_06, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_17_YANYUE:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_07, strategy.Strategylevel);
                        break;
                    }
                case CtiStrategyTypecodeCollection.STRATEGY_18_FANGYUAN:
                    {
                        openPoss = TacticalDao.getUserTacticalNextOpenPositions(userCode, SysTacticalTypecodeCollection.TACTICAL_08, strategy.Strategylevel);
                        break;
                    }
                default:
                    break;
            }

            foreach (Entities.Usr_Tacticalpos pos in openPoss)
                pos.Isopen = (SettingHelper.IntValue(true));

            // 历史次数

            history.Strategycount = (history.Strategycount + 1);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;
            command.Begin(delegate()
            {
                command.Update(queue);

                command.Update(profile);

                command.Update(strategy);

                command.Update(openPoss.ToArray());

                command.Update(history);

                command.Commit();
            });


            response.strategy = StrategyFacade.ToStrategy(strategy);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.queue = UsrQueueFacade.ToQueue(queue);
            response.Succeed = true;
            return response;
        }

    }

    public interface strategy_upgrade : ISangoResponse
    {
        player_profile profile { get;set;}

        player_strategy strategy { get;set;}

        player_queue queue { get;set;}
    }
}
